﻿package scripts {
	import com.foxaweb.pageflip.PageFlip;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.display.Shape;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.display.Loader;
	import flash.utils.Timer;
	import flash.events.TimerEvent;

	
	public class AXA_Geordi extends MovieClip {
		const PAGE_WIDTH = 240;
		const PAGE_HEIGHT = 180;
		var render:Shape=new Shape();
		const XML_URL:String = "asset/AXA.xml";
		static var _myXmlLoader:XMLLoader;
		static var myXML:XML;
		private var loadTimer:Timer;
		private var countPageLoaded:int = 0;
		var allswfs:Array=new Array();
		var pages:Array = new Array();
		
		
		
		
		public function AXA_Geordi() {
			_myXmlLoader = new XMLLoader(this.getAbsolutePath() + XML_URL);
			_myXmlLoader.addEventListener(Event.COMPLETE,xmlComplete);
		}
		protected function xmlComplete(e:Event):void
		{
			_myXmlLoader.removeEventListener(Event.COMPLETE,xmlComplete);
			myXML=_myXmlLoader.getXML();
			loadTimer=new Timer(100);
			loadTimer.addEventListener(TimerEvent.TIMER,swfLoadingTimerHandler);
			loadswf();
	
			
		}
		private function loadEnd(e:Event):void{
			trace(e.target.loader.content);
			countPageLoaded++;
		}
		private function loadswf():void{
			
			var pagelist:XMLList=myXML.pages.children();
			loadTimer.start();
			//trace(pagelist.length());
			for(var i:int=0;i<pagelist.length();i++){
				var pageRequest:URLRequest = new URLRequest(pagelist[i].attribute("src"));
				var contentLoader:Loader=new Loader();
				contentLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,loadEnd);
				contentLoader.load(pageRequest);
				allswfs[i]=contentLoader;
			}
		}
		protected function getAbsolutePath():String
		{
			var swfURL:String = loaderInfo.url;
			return swfURL.substr(0, swfURL.lastIndexOf("/") + 1);
		}
		private function swfLoadingTimerHandler(e:TimerEvent):void{
			trace(countPageLoaded);
			if(countPageLoaded == myXML.pages.children().length()){
				trace("AllLoaded");
				loadTimer.stop();
			}
		}
		private function splitSWFintoPages(){
			PageFlip.drawBitmapSheet(
		}
		
	}
	
}
